package com.huang.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class TalkSend  implements Runnable{


    private String tolocal;
    private int toPort;

    public TalkSend(String tolocal, int toPort) {
        this.tolocal = tolocal;
        this.toPort = toPort;

    }

    @Override
    public void run() {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        //从控制台输入 传输的数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true){
            String line = null;
            try {
                line = reader.readLine();
                byte[] msg = line.getBytes();
                DatagramPacket packet = new DatagramPacket(msg,msg.length, InetAddress.getByName(tolocal),toPort);

                socket.send(packet);
                if(line.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        socket.close();
    }


}
